using System.Collections.Generic;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;

namespace Game.Component
{
	public class EletricBrakeItem : BaseItem
	{
		public enum State
		{
			Open = 0,
			Close = 1
		}

		private State state;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		public override void ResetState()
		{
			base.ResetState();
			SetState((State)archiveData.state);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (state == State.Open)
			{
				SetState(State.Close);
				Singleton<AudioManager>.Instance.PlaySound("sound_eletric_brake_1");
			}
			else
			{
				SetState(State.Open);
				Singleton<AudioManager>.Instance.PlaySound("sound_eletric_brake_2");
			}
		}

		public void SetState(State val)
		{
			if (state == val)
			{
				return;
			}
			state = val;
			List<BaseItem> itemsByScriptType = Singleton<Scene>.Instance.GetItemsByScriptType(76);
			if (state == State.Open)
			{
				for (int i = 0; i < itemsByScriptType.Count; i++)
				{
					((LampItem)itemsByScriptType[i]).HadEletric(true);
				}
				model.Find("Open").SetObjectActive(true);
				model.Find("Close").SetObjectActive(false);
			}
			else
			{
				for (int j = 0; j < itemsByScriptType.Count; j++)
				{
					((LampItem)itemsByScriptType[j]).HadEletric(false);
				}
				model.Find("Open").SetObjectActive(false);
				model.Find("Close").SetObjectActive(true);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
